package color;

import java.io.Serializable;
import java.nio.FloatBuffer;

public class GLColor implements Serializable {
	private static final long serialVersionUID = 1393939L;

	public float r;
	public float g;
	public float b;
	public float a = 1.0f;

	public GLColor(GLColor color) {
		this.r = color.r;
		this.g = color.g;
		this.b = color.b;
		this.a = color.a;
	}

	public GLColor(FloatBuffer buffer) {
		this.r = buffer.get();
		this.g = buffer.get();
		this.b = buffer.get();
		this.a = buffer.get();
	}

	public GLColor(float r, float g, float b) {
		this.r = r;
		this.g = g;
		this.b = b;
		this.a = 1;
	}

	public GLColor(float r, float g, float b, float a) {
		this.r = Math.min(r, 1);
		this.g = Math.min(g, 1);
		this.b = Math.min(b, 1);
		this.a = Math.min(a, 1);
	}

	public GLColor(int r, int g, int b) {
		this.r = r / 255.0f;
		this.g = g / 255.0f;
		this.b = b / 255.0f;
		this.a = 1;
	}

	public GLColor(int r, int g, int b, int a) {
		this.r = r / 255.0f;
		this.g = g / 255.0f;
		this.b = b / 255.0f;
		this.a = a / 255.0f;
	}

	public GLColor(int value) {
		int r = (value & 0x00FF0000) >> 16;
		int g = (value & 0x0000FF00) >> 8;
		int b = (value & 0x000000FF);
		int a = (value & 0xFF000000) >> 24;

		if (a < 0) {
			a += 256;
		}
		if (a == 0) {
			a = 255;
		}

		this.r = r / 255.0f;
		this.g = g / 255.0f;
		this.b = b / 255.0f;
		this.a = a / 255.0f;
	}


	public static GLColor decode(String nm) {
		return new GLColor(Integer.decode(nm).intValue());
	}
	
	public int hashCode() {
		return ((int) (r + g + b + a) * 255);
	}

	public boolean equals(Object other) {
		if (other instanceof GLColor) {
			GLColor o = (GLColor) other;
			return ((o.r == r) && (o.g == g) && (o.b == b) && (o.a == a));
		}

		return false;
	}

	public String toString() {
		return "Color (" + r + "," + g + "," + b + "," + a + ")";
	}

	public GLColor darker() {
		return darker(0.5f);
	}

	/*white -255,255, 255, black 0,0,0*/
	public GLColor darker(float scale) {
		scale = 1 - scale;
		GLColor temp = new GLColor(r * scale, g * scale, b * scale, a);

		return temp;
	}


	public GLColor brighter() {
		return brighter(0.2f);
	}


	public GLColor brighter(float scale) {
		scale += 1;
		GLColor temp = new GLColor(r * scale, g * scale, b * scale, a);

		return temp;
	}
	
	public int getRed() {
		return (int) (r * 255);
	}

	public int getGreen() {
		return (int) (g * 255);
	}

	public int getBlue() {
		return (int) (b * 255);
	}

	public int getAlpha() {
		return (int) (a * 255);
	}
	
	public String toStringAsInt() {
		return "Color (" + getRed() + "," + getGreen() + "," + getBlue() + "," + getAlpha() + ")";
	}
}
